import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import TrainCourseItemContent from '@/components/TrainCourseItemContent' // secondary package based on el-pagination
import {getYearList,getTypeList,getTCList} from "@/api/training-course-list";
export default {
  name: "training-course-list",
  components: {
    Pagination,
    TrainCourseItemContent,
  },
  data() {
    return {
      types: "",
      years: [],
      publishYears: [],
      checkAll: false,
      yearList: [
        {
          key: "",
          value: "全部"
        },
      ],
      typesList: [
        {
          id: "",
          name: "全部"
        },
      ],
      listQuery: {
        orderName: "",
        orderSort: "",
        pageNum: 1,
        pageSize: 10,
        publishYear: "",
        publishYears: [],
        typeId: ""
      },
      total: 3,
      list:[],
      repairYear:"",
      searchYear:"",
    }
  },
  methods: {
    getList() {
      getTCList(this.listQuery).then((response) =>{
        this.list = response.data.list;
        this.total = Number(response.data.total)
      }).catch((err) =>{
        this.$message.error(err.message || 'error')
      });
    },
    getYear(){
      getYearList().then((response) =>{
        var arr = response.data;
        this.years = response.data;
        var obj = {};
        arr.forEach((item) =>{
          if (this.repairYear && item !== this.repairYear ){
            this.listQuery.publishYears.push(item);
          }
          if (this.searchYear && item == this.searchYear ){
            this.listQuery.publishYears.push(item);
          }
          obj = {
            key:item,
            value:item
          }
          this.yearList.push(obj)
        })
        this.checkAll = this.listQuery.publishYears.length === this.years.length
        this.getList();
      }).catch((err) =>{
        this.$message.error(err.message || 'error')
      });
    },
    getType() {
      var data = {
        type: "0",
        typeId: this.listQuery.typeId
      };
      getTypeList(data).then((response) =>{
        if (this.listQuery.typeId ==  "") {
          this.typesList = [{
            id: "",
            name: "全部"
          },...response.data];
        }else {

        }
      }).catch((err) =>{
        this.$message.error(err.message || 'error')
      });
    },
    handleCheckAllChange(val) {
      this.listQuery.publishYears = val ? this.years : [];
      this.getList();
    },
    change(value){
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.years.length;
      value.forEach((item) =>{
        if (item  === ''){
          this.listQuery.publishYears = this.years;
        }
      })
      this.getList();
    }

  },
  mounted(){
    this.repairYear = this.$utils.getParms(this, 'repairYear');
    this.searchYear = this.$utils.getParms(this, 'searchYear');
    this.getYear();
    this.getType();
  }

}
